const categoryService = require("../services/categoryService");
const result = require("../utils/result");

module.exports = {
  // 获取所有分类
  async getAllCategory(req, res) {
    try {
      const category = await categoryService.getAllCategory();
      res.json(result.success(category));
    } catch (error) {
      res.json(result.fail(error.message));
    }
  },
  // 创建分类
  async createCategory(req, res) {
    try {
      const category = await categoryService.createCategory(req.body);
      res.json(result.success(category));
    } catch (error) {
      res.json(result.fail(error.message));
    }
  },
  // 删除分类
  async deleteCategory(req, res) {
    try {
      const category = await categoryService.deleteCategory(req.params.id);
      res.json(result.success(category));
    } catch (error) {
      res.json(result.fail(error.message));
    }
  },
  // 更新分类
  async updateCategory(req, res) {
    try {
      const category = await categoryService.updateCategory(
        req.params.id,
        req.body
      );
      res.json(result.success(category));
    } catch (error) {
      res.json(result.fail(error.message));
    }
  },
};
